<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%@include file="/WEB-INF/ibnms/common/easyui.jsp" %>
<%@include file="/WEB-INF/ibnms/common/ztree.jsp" %>
<html style="height:100%;overflow:hidden;">
	<head>
		<jsp:include page="/WEB-INF/ibnms/common/configinclude.jsp"></jsp:include>
		<title>
		</title>
	</head>
	<script type="text/javascript">
		var $layout = $("#layout");
		var $zTreeObj;
		$(document).ready(function(){
			$zTreeObj = $.fn.zTree.init($("#roleManageTree"), 
			{	
				treeId:"id",
				data: {
					key:{
                           name:'name',
                           title:'name',
                           id:'id'
                       },
                       simpleData:{
                           idKey:'id'
                       }				
				},
				async:{
					enable:true,
					type:"post",
					dataType:"json",
					autoParam:["id=node.id"],
					url:"loadShortcutTreeNodeByType.do"
				},
				view: {
					fontCss: function (treeId, treeNode) {
						return (!!treeNode.highlight) ? {color:"#A60000", "font-weight":"bold"} : {color:"#333", "font-weight":"normal"};
					},
					dblClickExpand:true
				},
				callback:{
					onClick:function(event, treeId, treeNode, clickFlag){
						$layout.layout("resize");
						$("#addType").hide();
					},
					onAsyncSuccess:function(event, treeId, treeNode, msg){
						/*if(msg[0].pId==null||msg[0].pId=='null'){
							var firstNode = $zTreeObj.getNodeByParam('id',msg[0].id,null);
							$(firstNode).click();
							$zTreeObj.selectNode(firstNode,false);
							$("iframe[name='roleDisplayFrame']").attr("src","loadListRole.do?roleForm.PARENT_ID="+msg[0].id);
						}*/
					}
				}
			}, 
			 [
			]);
			
			
			
			var operateType = "";
			
			$("#newType").click(function(){
				$("#addType").show();
				$("#typeName").val('');
				operateType = "add";
			});
			
			$("#rename").click(function(){
				var nodes = $zTreeObj.getSelectedNodes();
				if(nodes.length==0){
					alert("请选择分类");
					return;
				}else{
					$("#addType").show();
					$("#typeName").val(nodes[0].name);
					operateType = "update";
				}
			});
			$("#del").click(function(){
				var nodes = $zTreeObj.getSelectedNodes();
				if(nodes.length==0){
					alert("请选择要删除的节点");
					return;
				}else{
					var id = nodes[0].id;
					if(nodes[0].isParent){
						$.ajax({
							url:"<%=application.getContextPath()%>/shorcut/deleteType.do?shortcutTypeForm.type_id="+id,
							type:"post",
							success:function(data){
								$zTreeObj.cancelSelectedNode(nodes[0]);
								$("#addType").hide();
								$zTreeObj.reAsyncChildNodes(null, "refresh");
							}
						});
					}else{
						$.ajax({
							url:"<%=application.getContextPath()%>/shorcut/delete.do?shortcutForm.shortcut_id="+id,
							type:"post",
							success:function(data){
								$zTreeObj.cancelSelectedNode(nodes[0]);
								$("#addType").hide();
								$zTreeObj.reAsyncChildNodes(null, "refresh");
							}
						});
					}
				}
			});
			
			$("#saveType").click(function(){
				var name = $("#typeName").val();
				if(name!=""){
					//name = encodeURIComponent(name);
					name = encodeURI(name);   
					name = encodeURI(name); 
					var id = "";
					if(operateType=="update"){
						var nodes = $zTreeObj.getSelectedNodes();
						id = nodes[0].id;
						if(nodes[0].pId==null){
							$.ajax({
								url:"<%=application.getContextPath()%>/shorcut/saveType.do?shortcutTypeForm.type_name="+name+"&shortcutTypeForm.type_id="+id,
								type:"post",
								success:function(data){
									$zTreeObj.reAsyncChildNodes(null, "refresh");
								}
							});
						}else{
							$.ajax({
								url:"<%=application.getContextPath()%>/shorcut/save.do?shortcutForm.shortcut_name="+name+"&shortcutForm.shortcut_id="+id,
								type:"post",
								success:function(data){
									$zTreeObj.reAsyncChildNodes(null, "refresh");
								}
							});
						}
					}else{
						$.ajax({
							url:"<%=application.getContextPath()%>/shorcut/saveType.do?shortcutTypeForm.type_name="+name,
							type:"post",
							success:function(data){
								$zTreeObj.reAsyncChildNodes(null, "refresh");
							}
						});
					}
					
				}
				$("#addType").hide();
			});
			
		});
		
	</script>
<body>
		<div class="easyui-layout" id="layout" style="width:100%;height:100%" >
			<div data-options="region:'south'" style="width:100%;height:60px;overflow:hidden;padding-left:0px;padding-top:0px;">
				<div id="addType" style="display:none">
					<input id="typeName" type="text" />
					<input id="saveType" type="button" class="btn-style02" align="middle" value="保存" />
				</div>
				<input id="newType" type="button" class="btn-style02" align="middle" value="新建分类" />
				<input id="rename" type="button" class="btn-style02" align="middle" value="重命名" />
				<input id="del" type="button" class="btn-style02" align="middle" value="删除" />
			</div>
			<div data-options="region:'center',title:''" style="width:95%;border-right: 0px;overflow-x:hidden;overflow-y:hidden">
				<ul id="roleManageTree" class="ztree" style="width:95%; height:98%; overflow:auto;"></ul>
			</div>
		</div>
</body>
</html>
